# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：PyMuPDF_model.py
#日期：2019-10-10
#备注：pip install PyMuPDF  
https://github.com/pymupdf/PyMuPDF
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import fitz
from operator import itemgetter
import os
import time
import glob
from pycacho.cachobase.file_deal import a_save_txt


# 将pdf转换为图片
def pdf_to_image(pdfPath, imagePath):
    pdfDoc = fitz.open(pdfPath)
    for pg in range(pdfDoc.pageCount):
        page = pdfDoc[pg]
        zoom = int(100)
        rotate = int(0)
        trans = fitz.Matrix(zoom / 75.0, zoom / 75.0).preRotate(rotate)         # 放大1倍
        pix = page.getPixmap(matrix=trans, alpha=False)
        if not os.path.exists(imagePath):
            os.makedirs(imagePath)
        pix.writePNG(imagePath+'/'+'OMARS1_%03d.png' % pg)     #将图片写入指定的文件夹内


# 将pdf转换为txt文字
def pdf_to_text(pdfPath, textPath):
    doc = fitz.open(pdfPath)
    for page in doc:
        text = page.getText()
        print(text)
        a_save_txt(textPath,text)


def pdf_to_TextBlocks(pdfPath, textPath):
    doc = fitz.open(pdfPath)
    for page in doc:
        blocks = page.getTextBlocks()
        sb = sorted(blocks, key=itemgetter(1, 0))
        for b in sb:
            print(b[4])

# 批量图片转换为PDF
def images_to_pdf():
    doc = fitz.open()
    new_pdf = 'F:\\alibaba\\temp\\01.pdf'
    for img in sorted(glob.glob(r'F:\\alibaba\\temp\\*.png')):
        img_doc = fitz.open(img)                           # 打开图片
        pdf_bytes = img_doc.convertToPDF()                  # 使用图片创建单页的 PDF
        img_pdf = fitz.open("pdf", pdf_bytes)
        doc.insertPDF(img_pdf)                              # 将当前页插入文档
    if os.path.exists(new_pdf):
        os.remove(new_pdf)
    doc.save(new_pdf)                               # 保存pdf文件
    doc.close()

# 图片转换为单个PDF
def image_to_pdf():
    i = 0
    for img in sorted(glob.glob(r'F:\image\output\new\01\*.jpg')):
        doc = fitz.open()
        img_doc = fitz.open(img)                           # 打开图片
        pdf_bytes = img_doc.convertToPDF()                 # 使用图片创建单页的 PDF
        img_pdf = fitz.open("pdf", pdf_bytes)
        doc.insertPDF(img_pdf,from_page=1, to_page=1)     # 将当前页插入文档
        i = i +1
        print(i)
        doc.save(str(i) + ".pdf")
        doc.close()

# 拆分成每一个pdf
def split_pdf(pdfPath):
    pdfDoc = fitz.open(pdfPath)
    for pg in range(pdfDoc.pageCount):
        doc = fitz.open()
        doc.insertPDF(pdfDoc, from_page=pg, to_page=pg)   # 第pg+1页
        doc.save(str(pg+1)+'.pdf')
        doc.close()


def main():
    sTime = time.time()
    #split_pdf('F:\\1.pdf')
    #pdf_to_image('F:\\alibaba\\temp\\OMARS HOT&NEW 2023.08.04(2).pdf', 'F:\\alibaba\\temp\\')
    #pdf_to_text('1.pdf', 'F:\image\pdftxt.txt')
    #pdf_to_TextBlocks('1.pdf', 'F:\image\pdftxt.txt')
    #image_to_pdf()
    images_to_pdf()
    eTime = time.time()
    s = eTime - sTime
    print('花费的时间为：%.2f秒' % (s))


if __name__ == '__main__':
    main()



